package com.project.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.MtTicketBean;
import com.project.bean.OrderBean;
import com.project.mapper.IOrderMapper;
import com.project.service.IOrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.List;
import java.util.Queue;

@Service
@Transactional
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private IOrderMapper orderMapper;

    @RabbitListener(queues = "queueOrder")
    public void joinOrder(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag, String json) throws IOException {
        //通知业务处理完成
        try {
            if (StringUtils.hasLength(json)){
                OrderBean orderBean=JSONObject.parseObject(json,OrderBean.class);
                System.out.println(orderBean);
                orderMapper.insert(orderBean);
            }
            channel.basicAck(tag,false); //tag标志，false-不批量确认
        } catch (Exception e) {
            channel.basicNack(tag,false,true);
        }
    }

    @Override
    public IPage<OrderBean> myOrderList(Integer pageNO, String username) {
        QueryWrapper<OrderBean> qw=new QueryWrapper<>();
        qw.like("o_userName",username);
        return orderMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public void updateStatus(Integer orderId) {
        OrderBean order=orderMapper.selectById(orderId);
        order.setStatus("已支付");
        orderMapper.updateById(order);
    }
}
